package com.coordinate.core.message.impl;

import java.util.Date; 
import java.util.List;

import javax.jdo.annotations.*;
 
import com.coordinate.core.message.Location;
import com.coordinate.core.message.TextMessage;
import com.coordinate.core.transport.Email;
import com.coordinate.core.transport.Sms;
import com.coordinate.core.transport.VoiceCall;
import com.coordinate.core.type.LangText;
import com.coordinate.core.message.impl.CMessage;
import com.coordinate.core.type.impl.CLangText;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;

import java.util.LinkedList;

@PersistenceCapable
public class CTextMessage extends CMessage implements 
TextMessage, Email, Sms, VoiceCall
{

	@Persistent(defaultFetchGroup="true")
	@Element(types=com.coordinate.core.type.impl.CLangText.class, dependent="true")
	private List<LangText> mMsgList;
	
	public CTextMessage()
	{
		this.mMsgList = new LinkedList<LangText>();
		System.out.println("CTextMessage Constructor called");
	}
	
	
	public LangText getLangText(String pCode) {
		
		   LangText lTempLangText = null;
			
			for(LangText lLangText : this.mMsgList)
			{
				 if(lLangText.getLangCode().equals(pCode))
				 {
					 lTempLangText = lLangText;
				 }
			}
			return lTempLangText;
		}


		public List<LangText> getMsgList() {
			
			return this.mMsgList;
		}


		public void setMsgList(List<LangText> pList) {
			this.mMsgList = pList;
			
		}



		public void add(LangText pLangText) {
			this.mMsgList.add(pLangText);
			
		}



		public void add(String pCode, Text pText) {
			
			add(new CLangText(pCode,pText));
		}
		
		/*E-mail --START */
       /*E-mail related stuff goes here*/
		public String getEmailKey() {
			
			return KeyFactory.keyToString(this.mKey);
		}
 
		/*The implementation of the 
		 * Email contents to be served to the Email 
		 * Service Provider goes here. */
		public String getEmailBody() {
			/*Format the text in the List and make a nice html body*/
			
			String lEmailBodyHeader = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
			String lHtmlStartTag = "<html xmlns=\"http://www.w3.org/1999/xhtml\">";
			String lHtmlEndTag ="</html>";
			String lBodyStartTag="<body bgcolor=\"ivory\">";
			String lBodyEndTag="</body>";
			
			String lEmailBody = lEmailBodyHeader + lHtmlStartTag+ lBodyStartTag;
			lEmailBody = lEmailBody +  "<div align=left><img src=\"http://egov-innovations.appspot.com/logo_govt.jpg\"></div>";
			
			lEmailBody = lEmailBody + "<table align=center cellspacing=20>";
			
			for(LangText lLangText: this.mMsgList)
			{
				lEmailBody = lEmailBody + "<tr><td>" + lLangText.getText().getValue() +"</td></tr>";
			}
			
			lEmailBody = lEmailBody + "</table></body></html>";
			
			return lEmailBody;
		}


		public List getEmailCCList() {
			// TODO Auto-generated method stub
			return null;
		}


		public List getEmailReceipientsList() {
		    List<String> lList = new LinkedList<String>();
			
			//lList.add("simith@ctg.ae");
			lList.add("simithn@yahoo.com");
			lList.add("simithn@gmail.com");
			lList.add("watchersparadise@gmail.com");
			lList.add("amithmit@yahoo.com");
			return lList;
		}


		public String getEmailSubjectLine() {
			// TODO Auto-generated method stub
			return this.mSubject;
		}


		public List getEmailToList() {
			/*Resolve the Email list via ContactsResolver*/
			
			return null;
		}
	
	public String getEmailFrom() {
			// TODO Auto-generated method stub
			return "simithn@gmail.com";
		}


	/*E-mail -- END */


	/* SMS Start*/	

		public String getSmsBody(String pLangCode) {
			// TODO Auto-generated method stub
			// Choose what exactly will go here, English / Arabic or both
			// 
			String lText = null;
			LangText lLangText = getLangText(pLangCode);
			if(null != lLangText)
			{
			   lText = lLangText.getText().getValue();
			}
			return lText;
		}


		public String getSmsFrom() {
			// TODO Auto-generated method stub
			return "3700";
		}


		public String getSmsKey() {
			// TODO Auto-generated method stub
			return KeyFactory.keyToString(this.mKey);
		}


		public List getSmsReceipientsList() {
			/** Get the Resolver from Amith and send the list**/
			List lList = new LinkedList();
			lList.add("971501325042");
			lList.add("971559266628");
			//lList.add("971506211757");
			return lList;
		}


		public String getSmsSubjectLine() {
			
			return this.mSubject;
		}


		public List<String> getPhoneNumbers() {
			// TODO Auto-generated method stub
			List<String> lList = new LinkedList<String>();
			lList.add("971501325042");
			lList.add("971559266628");
			//lList.add("971506211757");
			return lList;
		}


		public String getVoiceMessage() {
			// TODO Auto-generated method stub
			return getLangText("en").getText().getValue();
		}


		public String getVoiceMessageKey() {
			// TODO Auto-generated method stub
			return KeyFactory.keyToString(this.mKey);
		}
       
		/*SMS end*/
}